package com.chuangke.fss.fastdfs;

import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

import com.chuangke.common.exception.ChuangkeException;
import com.chuangke.file.model.FileBrief;
import com.chuangke.file.model.FileChunk;
import com.chuangke.fss.FSS;
import com.chuangke.fss.model.FSSObject;
import com.chuangke.fss.model.StorageType;

public class FSSFastDfs implements FSS {

	static {
		ClientGlobal.setG_connect_timeout(0);
		ClientGlobal.setG_network_timeout(0);
		ClientGlobal.setG_anti_steal_token(false);
		ClientGlobal.setG_charset("UTF-8");
	}
	private TrackerClient trackerClient;
	private TrackerServer trackerServer;
	private StorageClient storageClient;

	public FSSFastDfs() {
		try {
			ClientGlobal.init(this.getClass().getResource("/").getPath() + "fastdfs_config.properties");
			trackerClient = new TrackerClient();
			trackerServer = trackerClient.getConnection();
			storageClient = new StorageClient(trackerServer, null);
		} catch (Exception e) {
			throw new ChuangkeException(e);
		}
	}

	@Override
	public FSSObject upload(FSSObject fssObject) {
		fssObject.setStorageType(StorageType.fastdfs.name());
		try {
			String[] path = storageClient.upload_file(fssObject.getObjectContent(), fssObject.getSuffix(), null);
			fssObject.setStorage1(path[0]);
			fssObject.setStorage2(path[1]);
		} catch (Exception e) {
			throw new ChuangkeException(e);
		}
		return fssObject;
	}

	@Override
	public FSSObject download(FSSObject fssObject) {
		try {
			byte[] content = storageClient.download_file(fssObject.getStorage1(), fssObject.getStorage2());
			if (content == null) {
				throw new ChuangkeException("文件不存在");
			}
			fssObject.setObjectContent(content);
		} catch (Exception e) {
			throw new ChuangkeException(e);
		}
		return fssObject;
	}

	@Override
	public void delete(FSSObject fssObject) {
		try {
			storageClient.delete_file(fssObject.getStorage1(), fssObject.getStorage2());
		} catch (Exception e) {
			throw new ChuangkeException(e);
		}
	}

	@Override
	public FileBrief uploadAppenderFile(FileBrief fileBrief) {
		try {
			String[] storeages = storageClient.upload_appender_file(fileBrief.getFilename(), fileBrief.getSuffix(),
					null);
			fileBrief.setStoreage1(storeages[0]);
			fileBrief.setStoreage2(storeages[1]);

			return fileBrief;
		} catch (Exception e) {
			throw new ChuangkeException(e);
		}
	}

	@Override
	public boolean appenderFile(FileChunk fileChunk) {
		int result = -1;
		try {
			result = storageClient.append_file(fileChunk.getStoreage1(), fileChunk.getStoreage2(),
					fileChunk.getFile().getBytes());
		} catch (Exception e) {
			throw new ChuangkeException(e);
		}
		return 0 == result;
	}

	@Override
	public boolean mergeFile(FileBrief fileBrief) {
		int result = -1;
		try {
			result = storageClient.append_file(fileBrief.getStoreage1(), fileBrief.getStoreage2(),
					fileBrief.getFilename());
		} catch (Exception e) {
			throw new ChuangkeException(e);
		}
		return 0 == result;
	}

}
